package com.lin.mr.maizilaundry.fragmentorder;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.myapplication.MyApplication;
import com.lin.mr.maizilaundry.view.FlowLayout2;

import java.util.HashSet;

/**
 * 动态添加流式布局的标签
 */
public class FragmentPayed extends Fragment {
//    private FlowLayout flowLayout;
//    private String[] names = new String[]{
//            "沁园春","西江月","水龙吟","江城子","点绛唇","念奴娇","菩萨蛮","浣溪沙",
//            "临江仙","水调歌头","卜算子","满江红","永遇乐","虞美人"
//    };


//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//
//        View view = inflater.inflate(R.layout.fragment_payed, container, false);
//
//        flowLayout = (FlowLayout) view.findViewById(R.id.flow_layout);
//        initData();
//        return view;
//    }

//    private void initData() {
//        for (int i = 0; i < names.length; i++) {
//            Button button = new Button(getContext());
//            ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
//                    ViewGroup.LayoutParams.WRAP_CONTENT,
//                    ViewGroup.LayoutParams.WRAP_CONTENT
//            );
//            button.setText(names[i]);
//            flowLayout.addView(button,lp);


//        }
//        LayoutInflater inflater = LayoutInflater.from(getContext());
//        for (int i = 0; i < names.length; i++) {
//            //将flowlayout_item填充到flowLayout
//            TextView tv = (TextView) inflater.inflate(R.layout.flowlayout_item,flowLayout,false);
//            tv.setText(names[i]);
//            flowLayout.addView(tv);
//            tv.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(getActivity(), "人丑就该多读书", Toast.LENGTH_SHORT).show();
//                }
//            });
//        }
//    }

    //标签名称
    private HashSet<String> bookList = new HashSet<>() ;

    private String[] names = new String[]{
            "西游记", "围城", "骆驼祥子", "罗密欧与朱丽叶", "基督山伯爵", "许三观卖血记",
            "三国演义", "雷雨", "大卫·科波菲尔", "天龙八部", "阿Q正传",
            "月亮和六便士", "三体", "红楼梦", "动物农场"
    };
    //流式布局
    private FlowLayout2 mFlowLayout;
    //用于动态添加TextView到流式布局的按钮
    private Button mBtn;
    //接收新添加TextView的名称
    private EditText mEdit;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_payed2, container, false);

        mFlowLayout = (FlowLayout2) view.findViewById(R.id.flow_layout);
        init();

        mEdit = (EditText) view.findViewById(R.id.edit);
        mBtn = (Button) view.findViewById(R.id.btn);
        //点击按钮添加新标签
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newBook = mEdit.getText().toString();
                if (newBook.equals("")){ //判断输入的内容是否为空字符串
                    Toast.makeText(MyApplication.getContext(), "你输入的内容为空", Toast.LENGTH_SHORT).show();
                }else {
                    //加入新元素前的集合容量
                    int previousSize = bookList.size();
                    bookList.add(newBook);
                    //加入新元素后的集合容量
                    int afterSize = bookList.size();
                    //由于HashSet不允许重复元素，故可将加入新元素前后的集合容量进行判断，以确定新元素是否成功加入
                    if (previousSize == afterSize){
                        Toast.makeText(getContext(), "图书已在书单中", Toast.LENGTH_SHORT).show();
                    }else {
                        addTextView(newBook);
                        Toast.makeText(getContext(), "新图书已加入书单", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });
        return view;
    }

    public void init() {
        bookList.add("西游记");
        bookList.add("围城");
        bookList.add("骆驼祥子");
        bookList.add("罗密欧与朱丽叶");
        bookList.add("基督山伯爵");
        bookList.add("许三观卖血记");
        bookList.add("三国演义");
        bookList.add("雷雨");
        bookList.add("大卫·科波菲尔");
        bookList.add("天龙八部");
        bookList.add("月亮和六便士");
        bookList.add("阿Q正传");
        bookList.add("三体");
        bookList.add("红楼梦");
        bookList.add("动物农场");
        //遍历集合中的标签名称
        for (String s : bookList){
            addTextView(s);
        }
        //遍历标签名称数组
//        for (String s : names) {
//            addTextView(s);
//        }
    }

    public void addTextView(String newBook) {
        //加载TextView并设置名称
        TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.flowlayout_item, mFlowLayout, false);
        //将新标签添加到流式布局
        tv.setText(newBook);
        mFlowLayout.addView(tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "人丑就该多读书", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
